package com.appspot.categoryeditor.controller;

import java.util.ArrayList;
import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.appspot.categoryeditor.meta.CategoryMeta;
import com.appspot.categoryeditor.meta.FolderMeta;
import com.appspot.categoryeditor.meta.UserMeta;
import com.appspot.categoryeditor.model.Category;
import com.appspot.categoryeditor.model.Folder;
import com.appspot.categoryeditor.model.User;
import com.google.appengine.api.datastore.Key;

public class TestdataController extends Controller {

	final static UserMeta userMeta = UserMeta.get();
	final static FolderMeta FOLDER_META = FolderMeta.get();
	final static CategoryMeta categoryMeta = CategoryMeta.get();	
	
    @Override
    public Navigation run() throws Exception {
    	
    	prepareTestData();
    	
        return forward("jsp/testdata.jsp");
    }
    
    private User createUserModel(String userid){
		User user = new User();
		Key userKey = Datastore.createKey(userMeta, userid);
		user.setId(userKey);
		return user;
    }
    
    public void prepareTestData() {
//		User user1 = createUserModel("user1");
    		User user1 = createUserModel("categoryeditor1");


		List<Folder> folders = new ArrayList<Folder>();
		List<Key> folderKeys = new ArrayList<Key>();

		for (int i = 0; i < 5; i++) {
			Folder folder = new Folder();
			String id = "folder" + (i + 1);
			Key folderKey = Datastore.createKey(user1.getId(), FOLDER_META, id);

			folder.setId(folderKey);
			folder.setOrder(5 - i);
			
			folders.add(folder);
			folderKeys.add(folderKey);

		}

		List<Category> categories = new ArrayList<Category>();
		Key categoryKey1 = Datastore.createKey(
				user1.getId(),
				categoryMeta,
				"category1-1");
		Category category1 = new Category();
		category1.setId(categoryKey1);
		category1.setName("カテゴリ１-1");
		category1.setOrder(2);
		category1.setContent("category1 content");
		category1.setAccessible(true);
		category1.getFolderRef().setModel(folders.get(0));
		categories.add(category1);

		Key categoryKey2 = Datastore.createKey(
				user1.getId(),
				categoryMeta,
				"category1-2");
		Category category2 = new Category();
		category2.setId(categoryKey2);
		category2.setName("カテゴリ１-2");
		category2.setOrder(1);
		category2.setContent("category2 content");
		category2.getFolderRef().setModel(folders.get(0));
		categories.add(category2);

		Key categoryKey3 = Datastore.createKey(
				user1.getId(),
				categoryMeta,
				"category2-1");
		Category category3 = new Category();
		category3.setId(categoryKey3);
		category3.setName("カテゴリ2-1");
		category3.setOrder(1);
		category3.setContent("category3 content");
		category3.getFolderRef().setModel(folders.get(1));
		categories.add(category3);

		Key categoryKey4 = Datastore.createKey(
				user1.getId(),
				categoryMeta,
				"category3-1");
		Category category4 = new Category();
		category4.setId(categoryKey4);
		category4.setName("カテゴリ3-1");
		category4.setOrder(1);
		category4.setContent("[{\"data\":\"content\"}]");
		category4.getFolderRef().setModel(folders.get(2));
		categories.add(category4);

		Datastore.put(user1);
		Datastore.put(folders);
		Datastore.put(categories);
		
//		Datastore.put(createUserModel("categoryeditor1"));

	}
    
    
}
